<template>
  <div>
    <el-card class="w_100" v-loading="spinning">
      <template #header>
        <header>
          <div class="left">街道/镇用户数量统计</div>
          <div class="right">
            <el-button @click='handleExportXlsMy' type="primary" size="small">下载</el-button>
            <el-select v-model="queryParam.selectUnitId" size="mini" style="width: 120px;margin-left:10px;"
                       @change='unitIdClick' placeholder="请选择区域"
                       v-hasPermi="['nongjt:user:list']" >
              <el-option v-for="{id,area} of unitList" :key="id" :label="area" :value="id">
              </el-option>
            </el-select>
          </div>
        </header>
      </template>

      <div id='StreetUserCountCharts'  style='height: 300px;width: 100%;'></div>
    </el-card>
  </div>
</template>

<script>
import {echartResizeMixins} from "@/views/certificate/charts/echartResizeMixins";
import {getAction, getActionSimple} from "@/utils/axiosUtils";
import {mapGetters} from "vuex";

const echarts = require('echarts')

export default {
  name: 'StreetUserCountCharts',
  mixins: [echartResizeMixins],
  data() {
    return {
      description: '街道/镇用户数量',
      url: {
        streetUserCountStatistics: '/certificate/certificate_count/streetUserCountStatistics',
        exportXlsUrl: '/certificate/certificate_count/exportXlsStreetUserCountStatistics',
        userStreetList: '/certificate/certificate_count/getUserStreetAll',
      },
      title: '街道/镇用户数量统计',
      //加载动画相关
      Xindex:null,
      spinning: false,

      //查询参数
      queryParam: {
        //选中的单位id
        selectUnitId: null
      },
      //图表相关
      chart: null,
      //总量
      total:0,
    }
  },
  mounted() {
    this.queryParam.selectUnitId = this.selectUnitId
    setTimeout(() => {
      let param = { unitId:this.queryParam.selectUnitId};
      this.initChart(param)
    }, 500);
  },
  methods: {
    //初始化图表
    async initChart(param) {
      if (!this.chart) {
        const dom = document.querySelector('#StreetUserCountCharts')
        this.chart = echarts.init(dom)
        this.chart.on('click', params => {
          const {data} = params
          this.$router.push({path: '/user/perfect?country=' + data.name});
        })
      }
      let result = await getActionSimple(this.url.streetUserCountStatistics, param)
      let option = {
        title: {
          text: '街道/镇用户数量统计',
          subtext: '',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} <br /> {c}人 {d}%',
        },
        //保存为图片，等其他功能,在右上角
        toolbox: {
          feature: {
            saveAsImage: {show: true}
          }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: '50%',
            data: result,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              show: true,
              // formatter: '{b} ({c}户; {d}%)',
              formatter: params => {
                let {name, value, percent} = params
                return `${name}(${value}户 ${percent ? percent : 0}%)`
              },
              color: () => {
              }
            }
          }
        ]
      };
      this.chart.setOption(option)
    },
    //区域选择事件
    unitIdClick(n) {
      if (n) {
        let param = { unitId: n }
        this.initChart(param)
        //设置图标
        const { area } = this.unitList.find(t => t.id === n)
        this.area = area
      }
    },
    //导出
    async handleExportXlsMy() {
      this.spinning = true
      try {
        let param = {
          unitId:this.queryParam.selectUnitId,
        }
        console.log("导出参数", param)
        this.download(this.url.exportXlsUrl, {
          ...param
        }, `区域合格证开具数量排名.xlsx`)
      } finally {
        this.spinning = false
      }
    }
  },
  computed: {
    ...mapGetters([
      'unitList',
      'selectUnitId',
    ]),
  },
  watch:{
    unitList(n){
      if (n){
        this.unitIdClick(this.queryParam.selectUnitId)
      }
    },
    selectUnitId(n){
      if (n){
        this.queryParam.selectUnitId = n
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.w_100{
  width: 100%;
}
header{
  display: flex;
  justify-content: space-between;
  align-content: center;

  .left{
    font-weight: bold;
  }
}
</style>
